
from django.db import models
from django.db.models.enums import Choices
from DjangoUeditor.models import UEditorField
"""
项目类型，例如BTC，ETH，POLKADOT等
"""
class ProjectType(models.Model):
    # 项目名称
    name = models.CharField(max_length = 20, blank = True, null = True, default = '', unique = True) 
    class Meta:
        db_table = 'ProjectType'
        ordering = ('id', )
    # 管理页面中的显示
    def __str__(self):
        return self.name

"""
项目内容
"""
class Project(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 项目名称
    name = models.CharField(max_length = 100, unique = True, help_text = '项目名称') 
    # 是否发布
    is_publish = models.BooleanField(help_text = '是否发布')
    # 发布时间
    publish_time = models.DateTimeField(help_text = '发布时间')
    # 项目logo
    icon = models.ImageField(upload_to = 'rest_static/project_image', help_text = '项目logo')
    # 项目类型
    project_type = models.ForeignKey("ProjectType", on_delete = models.CASCADE, help_text = '项目类型')
    # 项目介绍
    intruduction = UEditorField(imagePath = "rest_static/ueditor/project_intruduction/", filePath = "rest_static/ueditor/project_intruduction/", help_text = '项目介绍')
    # 团队介绍
    team = UEditorField(imagePath="rest_static/ueditor/project_team/", filePath="rest_static/ueditor/project_team/", help_text = '团队介绍')
    
    class Meta:
        db_table = 'Project' 
        ordering = ('-publish_time', ) # 按照发布时间逆序
    # 管理页面中的显示
    def __str__(self):
        return self.name

"""
用户信息
"""
class UserInfo(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 用户地址
    address = models.CharField(max_length=100, unique = True, help_text = "用户地址")
    # 用户昵称
    name = models.CharField(max_length = 100, unique = True, default='', help_text = '用户昵称') 
    # token,最后登录返回的token
    token = models.CharField(max_length = 200, unique = True, default = '', help_text = '最后登录返回的token')
    class Meta:
        db_table = 'UserInfo'
        ordering = ('-id',)
    #管理页面中的显示
    def __str__(self):
        return self.name

"""
投资分析文章
"""
class InvestmentAnalyse(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 文章名称
    title = models.CharField(max_length = 100, default='', help_text = '文章名称')
    # 文章标题图片
    icon = models.ImageField(upload_to = 'rest_static/project_investment_analyse/', help_text = '文章标题图片')
    # 所属项目
    project = models.ForeignKey('Project', default = None, on_delete = models.CASCADE, help_text = '所属项目')
    # 是否发布
    is_publish = models.BooleanField(default=False, help_text = '是否发布')
    # 发布时间
    publish_time = models.DateTimeField(default='', help_text = '发布时间')
    # 作者
    author = models.ForeignKey('UserInfo', default = None, on_delete = models.DO_NOTHING, help_text = '作者信息')
    # 文章内容
    content = UEditorField(imagePath="rest_static/ueditor/project_investment_analyse/", filePath="rest_static/ueditor/project_investment_analyse/", help_text = '团队介绍')
    class Meta:
        db_table = 'InvestmentAnalyse' 
        ordering = ('-id',)


"情报文章"
class InfoPage(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 文章名称
    title = models.CharField(max_length = 100, default='', help_text = '文章名称')
    # 文章标题图片
    icon = models.ImageField(default=None, blank = True, null = True, upload_to = 'rest_static/info_page/', help_text = '文章标题图片')
    # 所属文件夹
    info_folder = models.ForeignKey('InfoFolder', default = None, null = True, blank=True, on_delete = models.CASCADE, help_text = '所属项目')
    # 所属项目
    project = models.ForeignKey('Project', on_delete = models.CASCADE, help_text = '所属项目')
    # 是否发布
    is_publish = models.BooleanField(default=False, help_text = '是否发布')
    # 发布时间
    publish_time = models.DateTimeField(default='', help_text = '发布时间')
    # 作者
    author = models.ForeignKey('UserInfo', default = None, on_delete = models.DO_NOTHING, help_text = '作者信息')
    # 文章内容
    content = UEditorField(imagePath="rest_static/ueditor/info_page/", filePath="rest_static/ueditor/info_page/", help_text = '团队介绍')
    # 价格
    price = models.DecimalField(default = 0, max_digits=20, decimal_places=2, help_text="价格")
    # 文章类型
    ContentType = (
        (0, 'Private'),
        (1, 'Public'),
        (2, 'Sell'),
    )
    content_type = models.IntegerField(choices = ContentType, default= 0, help_text="文章类型, 0, private   1, public   2, sell")
    # 查看和购买是否需要nft
    need_nft = models.BooleanField(default = True, help_text = '查看文章是否需要nft')
    
    # 是否数量限售
    is_sell_limit = models.BooleanField(default = False, help_text = '文章是否限售')
    # 限售总数
    sell_limit_count = models.IntegerField(default = 0, help_text = '限售总数')
    # 售出总数
    sell_count = models.IntegerField(default = 0, help_text = '限售总数')

    # 是否时间限售
    is_time_limit = models.BooleanField(default = False, help_text = '是否时间限售')
    # 时间限制
    time_limit = models.DateTimeField(default = None, null = True, blank = True, help_text = '时间限制')
    class Meta:
        db_table = 'InfoPage' 
        ordering = ('-publish_time',)

"""
情报文章购买记录
"""
class InfoTranslateHistory(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 用户名称
    user_id = models.ForeignKey("UserInfo", on_delete = models.CASCADE, help_text = '用户id')
    # 文章id
    info_id = models.ForeignKey("InfoPage", on_delete = models.CASCADE, help_text = '文章id')
    # 交易hash
    txhash = models.CharField(default = '', max_length = 100, unique = True, help_text = '交易hash') 
    # 金额
    amount = models.DecimalField(default = 0, max_digits=20, decimal_places=10, help_text="金额")
    class Meta:
        db_table = 'InfoTranslateHistory'

"""
情报信息文件夹
"""
class InfoFolder(models.Model):
    # 创建时间
    created = models.DateTimeField(auto_now_add = True, help_text = '创建时间')
    # 用户名称
    user_id = models.ForeignKey("UserInfo", on_delete = models.CASCADE, help_text = '用户id')
    # 文件夹名称
    folder_name = models.CharField(default = '', max_length = 100, unique = True, help_text = '文件夹名称') 
    class Meta:
        db_table = 'InfoFolder'
        indexes = [
            models.Index(
                fields=['user_id', 'folder_name'],
                name = 'name_idx',
            ),
        ]
